package test;

/**
 * @author WYC
 * @version 1.0
 * @ClassName Animal
 * @date 2024/11/14 1:12
 * @description
 */
abstract class Animal {
    protected String name;
    protected String species; // 种类
    protected String breed;   // 品种
    protected double price;


    public Animal(String name, String species, String breed, double price) {
        this.name = name;
        this.species = species;
        this.breed = breed;
        this.price = price;
    }

    // 吃饭的方法
    public void eat() {
        System.out.println(name + "在吃饭");
    }

    // 睡觉的方法
    public void sleep() {
        System.out.println(name + "在睡觉");
    }

    // 定义一个叫的抽象方法，由子类实现
    public abstract void call();

    // 玩耍的方法
    public void play() {
        System.out.print(name + "在玩耍时");
        call(); // 调用叫的方法
    }
}

// 小猫类
class Cat extends Animal {
    private boolean isPlaying = false; // 用于记录小猫是否正在玩耍

    public Cat(String name, String species, String breed, double price) {
        super(name, species, breed, price);
    }



    //子类实现抽象方法 重写叫的方法
    @Override
    public void call() {
        if (isPlaying) {
            System.out.println("喵喵叫");
        }
    }

    // 重写玩耍方法
    @Override
    public void play() {
        isPlaying = true;
        super.play();

    }



    // 让猫停止玩耍的办法
    public void stopPlaying() {
        isPlaying = false;
    }
}

// 小狗类
class Dog extends Animal {
    public Dog(String name, String species, String breed, double price) {
        super(name, species, breed, price);
    }

    //和小猫类一样，子类实现抽象方法 重写叫的方法
    @Override
    public void call() {
        System.out.println(name + "汪汪叫");
    }

    // 重写玩耍方法，小狗玩耍时也会叫
    @Override
    public void play() {
        super.play();

    }
}

public class Main {
    public static void main(String[] args) {
        // 实例化两个小猫对象
        Animal cat1 = new Cat("小红", "Cat", "狮子猫", 2000.0);
        Cat cat2 = new Cat("丢丢", "Cat", "布偶", 3000.0);




        // 实例化两个小狗对象
        Animal dog1 = new Dog("娜美", "Dog", "金毛", 500.0);
        Dog dog2 = new Dog("小迪", "Dog", "柯基", 400.0);

        // 调用方法进行测试 两只小猫
        cat1.eat();
        cat1.sleep();
        cat1.play();
        System.out.println();

        cat2.eat();
        cat2.sleep();
        cat2.play();
        System.out.println();

        cat2.stopPlaying();// 小猫停止玩耍后不会叫
        cat2.play();
        System.out.println();

        // 两只小狗
        dog1.eat();
        dog1.sleep();
        dog1.call();//小狗不只是在玩的时候才叫
        dog1.play();
        System.out.println();

        dog2.eat();
        dog2.sleep();
        dog2.call();
        dog2.play();


    }
}
